<?php
error_reporting(E_ALL ^ E_NOTICE);
if ($_SERVER['argc']<4) {
    echo 'php ',$_SERVER['argv'][0],' LocalSettings.php old_name new_name',"\n";
    die();
}
$f = $_SERVER['argv'][1];
if (!file_exists($f)) {
    echo 'file ',$f," not exists\n";
    exit(109);
}

$old_name=$_SERVER['argv'][2];
$new_name=$_SERVER['argv'][3];
if ($old_name==$new_name) {
    echo "tow are same\n";
    exit(110);
}


if (!defined('MEDIAWIKI')) {
    define('MEDIAWIKI',1);
}
require_once($f);

while (true) {
    echo 'Change ',$old_name,' to ',$new_name,"? [yes/no]:";
    $answer = fgets(STDIN);
    $answer = strtolower(trim($answer));
    if ($answer == 'no') {
        exit();
    }
    if ($answer == 'yes') {
        break;
    }
}
mediawiki_reusername($old_name,$new_name);
function mediawiki_reusername($old_name,$new_name) {
    global $wgDBserver,$wgDBuser,$wgDBpassword,$wgDBname,$wgDBprefix;
    mysql_connect($wgDBserver, $wgDBuser, $wgDBpassword);
    mysql_select_db($wgDBname);
    $old_name = mysql_real_escape_string($old_name);
    $new_name = mysql_real_escape_string($new_name);

    $table_info = array(
        array('recentchanges','rc_user_text'),
        array('revision','rev_user_text'),
        array('user','user_name'),
    );

    if ($wgDBmysql5) {
        mysql_query('SET NAMES UTF8');
    }
    foreach ($table_info as $info) {
        $sql = "UPDATE {$wgDBprefix}{$info[0]} SET {$info[1]}='{$new_name}' WHERE {$info[1]}='{$old_name}'";
        mysql_query($sql);
    }
    $sql = "UPDATE {$wgDBprefix}page SET page_title='{$new_name}' WHERE page_namespace=2 and page_title='{$old_name}'";
    mysql_query($sql);
    return true;
}
